using System;
using System.Collections.Generic;
using System.Linq;
using Mvvm.Models;

namespace Mvvm.Services;

public class PersonalityProfileService
{
    public double SociabilityScore { get; set; }
        public double EmotionalStabilityScore { get; set; }
        public double OpennessScore { get; set; }
        public double ConscientiousnessScore { get; set; }
        public double NeuroticismScore { get; set; }
        
        public int flag{ get; set; } 
        
        public string user1Name { get; set; }
        public string user2Name { get; set; }
        public string stringReport { get; set; }

        public PersonalityProfileService(ChatProcessedData chatData, int flag)
        {
            this.flag = flag;
            // 社交性评分：通过总消息数、聊天时长等评估
            SociabilityScore = CalculateSociability(chatData);
            
            // 情绪稳定性评分：通过消极情绪数量（愤怒、悲伤等）计算
            EmotionalStabilityScore = CalculateEmotionalStability(chatData);
            
            // 开放性评分：通过积极情绪（如愉快、喜爱）和高频词分析
            OpennessScore = CalculateOpenness(chatData);
            
            // 责任感评分：基于用户的回复时间
            ConscientiousnessScore = CalculateConscientiousness(chatData);
            
            // 神经质评分：基于负面情绪的表现
            NeuroticismScore = CalculateNeuroticism(chatData);
            user1Name = chatData.Username1;
            user2Name = chatData.Username2;
            
        }

        private double CalculateSociability(ChatProcessedData chatData)
        {
            double user1Messages = chatData.User1MessageCount;
            double user2Messages = chatData.User2MessageCount;
            double user1Duration = chatData.User1AverageReplyTime;
            double user2Duration = chatData.User2AverageReplyTime;
            double averageDuration = chatData.AverageDuration;
            if (flag != 1)
                // 假设社交性与聊天总时长和消息数量成正比
                return user1Messages * 0.5 + user1Duration * 0.3 + averageDuration * 0.2;
            return user2Messages * 0.5 + user2Duration * 0.3 + averageDuration * 0.2;
        }

        private double CalculateEmotionalStability(ChatProcessedData chatData)
        {
            // 区分两个不同的用户，返回不同的计算结果
            // 通过负面情绪的数量来推算情绪稳定性
            double user1NegativeEmotions = chatData.User1NegativeMessageCount + chatData.User1AngryMessageCount + chatData.User1SadMessageCount;
            double user1PositiveEmotions = chatData.User1PositiveMessageCount + chatData.User1HappyMessageCount + chatData.User1LoveMessageCount;
            double user2NegativeEmotions = chatData.User2NegativeMessageCount + chatData.User2AngryMessageCount + chatData.User2SadMessageCount;
            double user2PositiveEmotions = chatData.User2PositiveMessageCount + chatData.User2HappyMessageCount + chatData.User2LoveMessageCount;

            // 情绪稳定性 = 正面情绪占比 / 负面情绪占比
            if(flag!=1)
                return user1PositiveEmotions / (user1NegativeEmotions + 1); // +1 防止除以0
            return user2PositiveEmotions / (user2NegativeEmotions + 1);
        }

        private double CalculateOpenness(ChatProcessedData chatData)
        {
            // 假设开放性与愉快和爱等情感相关，和高频词的多样性相关
            double user1opennessScore = chatData.User1LoveMessageCount * 0.4 + chatData.User1HappyMessageCount * 0.6;
            double user2opennessScore = chatData.User2LoveMessageCount * 0.4 + chatData.User2HappyMessageCount * 0.6;
            
            var uniqueHighFrequencyWords = chatData.User1HighFrequencyWords.Split(',').Distinct().Count()
                                                + chatData.User2HighFrequencyWords.Split(',').Distinct().Count();
            if(flag!=1)
                return user1opennessScore + uniqueHighFrequencyWords * 0.2;
            return user2opennessScore + uniqueHighFrequencyWords * 0.2;
        }

        private double CalculateConscientiousness(ChatProcessedData chatData)
        {
            // 通过回复速度来评估责任感
            double user1Responsiveness = chatData.User1AverageReplyTime;
            double user2Responsiveness = chatData.User2AverageReplyTime;
            return flag!=1 ? user1Responsiveness : user2Responsiveness;
        }

        private double CalculateNeuroticism(ChatProcessedData chatData)
        {
            // 神经质与负面情绪相关（愤怒、恐惧、悲伤等）
            double user1NeuroticismScore = chatData.User1AngryMessageCount + chatData.User1FearMessageCount + chatData.User1SadMessageCount;
            double user2NeuroticismScore = chatData.User2AngryMessageCount + chatData.User2FearMessageCount + chatData.User2SadMessageCount;
            return flag!=1 ? user1NeuroticismScore : user2NeuroticismScore;
        }

        public List<double> GenerateReport()
        {
            // 将控制台打印的数据保存到stringReport中
            if(flag==0)
                stringReport = $"                    {user1Name}人物性格报告\n";
            else
                stringReport = $"                    {user2Name}人物性格报告\n";
            stringReport     +=
                           "------------------------------------------------\n" +
                           $"1. 社交性（Sociability）：{SociabilityScore:F2}\n" +
                           $"   描述：该人物在社交活动中表现较{(SociabilityScore > 50 ? "活跃" : "低调")}.\n" +
                           $"2. 情绪稳定性（Emotional Stability）：{EmotionalStabilityScore:F2}\n" +
                           $"  描述：该人物的情绪比较 {(EmotionalStabilityScore >
                                                              1.5 ? "稳定" : "不稳定")}，较少受到负面情绪影响。\n" +
                           $"3. 开放性（Openness）：{OpennessScore:F2}\n" +
                           $"   描述：该人物对新事物和新体验非常 {(OpennessScore > 30 ? "开放" : "保守")}，具有探索精神。\n" +
                           $"4. 责任感（Conscientiousness）：{ConscientiousnessScore:F2}\n" +
                           $"   描述：该人物做事非常 {(ConscientiousnessScore < 10 ? "随意" : "有责任心")}，对工作充满热情。\n" +
                           $"5. 神经质（Neuroticism）：{NeuroticismScore:F2}\n" +
                           $"   描述：该人物在压力面前较 {(NeuroticismScore > 15 ? "容易焦虑" : "情绪稳定")}，不容易受困扰。\n" +
                           "------------------------------------------------\n";
            Console.WriteLine(stringReport);
            return new List<double>() { SociabilityScore, EmotionalStabilityScore, OpennessScore, ConscientiousnessScore, NeuroticismScore };
        }
        
}